<?php

require_once dirname(__FILE__)."/provider.php";
require_once dirname(__FILE__)."/../db/db.php";
require_once dirname(__FILE__)."/_netfonds.php";

define("TBLPROVIDER_NAME", "tblProvider");

define("TBLPROVIDER_FLD_PROVNAME", "ProviderName");
define("TBLPROVIDER_FLD_SINGLETICKTYPE", "SingleTick");
define("TBLPROVIDER_FLD_RANK", "Rank");
define("TBLPROVIDER_FLD_MAP", "FieldMap");


class TblProvider extends dbTable{
	public function __construct(){
		$keyValue = array(TBLPROVIDER_FLD_PROVNAME, TBLPROVIDER_FLD_SINGLETICKTYPE, TBLPROVIDER_FLD_MAP, TBLPROVIDER_FLD_RANK);
		
		parent::__construct(TBLPROVIDER_NAME, $keyValue);
	}
	
	public function CreateTable(){
		$sql = "CREATE TABLE $this->name (".TBLPROVIDER_FLD_PROVNAME." VARCHAR(20) NOT NULL, ";
		$sql .= TBLPROVIDER_FLD_RANK." INT NOT NULL, ";
		$sql .= TBLPROVIDER_FLD_SINGLETICKTYPE." BOOLEAN NOT NULL, ";
		$sql .= TBLPROVIDER_FLD_MAP." VARCHAR(500) NOT NULL, ";
		$sql .= "PRIMARY KEY(".TBLPROVIDER_FLD_RANK."))";
		
		$this->db->query($sql);
	}
	
	protected function object2record($provObj){
		$arrAssocVal = array();
		$arrAssocVal[TBLPROVIDER_FLD_PROVNAME] = $provObj->GetName();
		$arrAssocVal[TBLPROVIDER_FLD_SINGLETICKTYPE] = $provObj->GetType();
		$arrAssocVal[TBLPROVIDER_FLD_MAP] = base64_encode(serialize($provObj->GetMap()));
		$arrAssocVal[TBLPROVIDER_FLD_RANK] = $provObj->GetRank();
		if($arrAssocVal[TBLPROVIDER_FLD_RANK] == 0){		
			//ranking automatico
			$sql = "SELECT * FROM ".TBLPROVIDER_NAME." WHERE 1=1 ORDER BY ".TBLPROVIDER_FLD_RANK." DESC LIMIT 1";
			$result = $this->db->query($sql);
			if(isset($result)){
				$tmpArr = $this->db->getAssocArray($result);
				$arrAssocVal[TBLPROVIDER_FLD_RANK] = $tmpArr[TBLPROVIDER_FLD_RANK] + 1;
			}
			else
				$arrAssocVal[TBLPROVIDER_FLD_RANK] = 1;
		}
		
		return $arrAssocVal;
	}
	
	protected function record2object($assocArrayRec){
		$provClassName = $assocArrayRec[TBLPROVIDER_FLD_PROVNAME];
		$mapDec = base64_decode($assocArrayRec[TBLPROVIDER_FLD_MAP]);
		if(class_exists($provClassName))
			return new $provClassName(	$assocArrayRec[TBLPROVIDER_FLD_SINGLETICKTYPE],
										unserialize($mapDec),
										$assocArrayRec[TBLPROVIDER_FLD_RANK]
									);
		else
			return NULL; 
	}
	
	/**
	 * aggiunge il provider in lista
	 * @param Provider $provObj: istanza del provider da aggiungere
	 */
	public function AddProvider(Provider $provObj){
		$arrAssocVal = $this->object2record($provObj);
		parent::Insert($arrAssocVal);
	}
	
	/**
	 * restituisce il provider con rank indicato
	 * @param {1 ... nTotProvider} $rank: 1 => il miglior provider in lista; nProvider => il peggior provider in lista
	 */
	public function GetProviderByRank($rank){
		$sql = "SELECT * FROM ".TBLPROVIDER_NAME." WHERE ".TBLPROVIDER_FLD_RANK." = $rank";
		$result = $this->db->query($sql);
		if(isset($result)){
			return $this->record2object($this->db->getAssocArray($result));
		}
		return NULL;
	}
}



//////////////////
/// UNIT TEST
//////////////////

class tblProviderTest{
	protected $tblProv = NULL;
	
	public function __construct(){
		$this->tblProv = new TblProvider();
	}
	
	public function Test(){
		$provItemC = new Netfonds(1);

		$this->tblProv->AddProvider($provItemC);
		$provItemRead = $this->tblProv->GetProviderByRank(1);
		$qr = $provItemRead->Query("AAPL.O", time());
		$testStock = $provItemRead->Generate($qr->GetRow(1));
		print_r($testStock);		
		$this->tblProv->Show();
	}
}

?>
